var editor = {
	cursor: {
		lastx: 0,
		lasty: 0,
		x: 0,
		y: 0
	},
	tool: {
		size: 1,
		
	}
}

function drawEditor() {
	drawEditorCursor();
	//updateEditorTool();
}

function mouseOn(xs, ys, h) {
	var g = game.grid;
	var x = game.grid*xs-game.grid*ys + camera.x + 3;
	var y = game.grid/2*(xs+ys) - (h-32)*4 + camera.y + 3;

	var offsetX = Math.abs(mouse.x - (x + g));
	var offsetY = Math.abs(mouse.y - (y + g/2));
	
	if (offsetX + offsetY*2 <= g+1) {
		editor.cursor.x = xs;
		editor.cursor.y = ys;
	}
	/*if (mouse.x > x && mouse.x < x + g*2 &&
		mouse.y > y && mouse.y < y + g) {*/
	//}
}

function updateEditor() {
	editor.cursor.lastx = editor.cursor.x;
	editor.cursor.lasty = editor.cursor.y;
	
	updateEditorTool();
	
	editor.cursor.x = -1;
	editor.cursor.y = -1;
}

function updateEditorTool() {
	if (mouse.press) {
		var x = editor.cursor.lastx;
		var y = editor.cursor.lasty;
		if (x>-1 && y>-1 && x<content.level.map.length && y<content.level.map[0].length) {
			var tile = content.level.map[x][y];
			var eindex = parseInt(tile.substr(0, 2));
			var h = parseInt(tile.substr(2, 2));
			
			h++;
			
			var hstr = gridString(h);
			var estr = gridString(eindex);
			
			content.level.map[x][y] = estr + hstr;
		}
		//alert(content.level.map[x][y]);
	}
}

function gridString(gridInt) {
	s = gridInt.toString();
	if (s.length < 2) {
		s = '0' + s;
	}
	return s;
}

function drawEditorCursor() {
	i = editor.cursor.lastx;
	j = editor.cursor.lasty;
	
	if (i>-1 && j>-1 && i<content.level.map.length && j<content.level.map[0].length) {
		game.ctx.save();
		game.ctx.globalAlpha = 0.3;
		
		
		var h = parseInt(content.level.map[i][j].substr(2, 2));
		drawIsoImage(content.images["MaskSprite1"], 
			0, 0, 
			game.grid*i-game.grid*j, 
			game.grid/2*(i+j) - (h-32)*4,
			0.5,
			1
			);
	
		game.ctx.restore();
	}
}

function drawGrid() {
	//selected grid
	/*
	game.ctx.fillStyle = "rgba(100, 255, 255, 0.5)"; 
	var mouseGridPos = mouseGrid();
	game.ctx.fillRect(mouseGridPos.x * game.grid,mouseGridPos.y * game.grid,
		game.grid,game.grid);
	*/
	//game.ctx.strokeStyle = "rgba(0, 0, 0, 0.05)"; 
	var xn=0;
	//horisontaalne
	for (i = 0; i < (game.canvas.width / game.grid); i++) {
		var j = i * game.grid - Math.round(camera.x / game.grid) * game.grid;
		game.ctx.moveTo(j, -camera.y);
		game.ctx.lineTo(j, game.canvas.height);
	}
	for (i = 0; i < (game.canvas.height / game.grid); i++) {
		var j = i * game.grid - Math.round(camera.y / game.grid) * game.grid;
		game.ctx.moveTo(-camera.x, j);
		game.ctx.lineTo(game.canvas.width, j);
	}
	game.ctx.fillText("n: " + xn, 10, 40);
	xn = 0;
	//game.ctx.stroke();
}